package com.yuzhi.common.jackson;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.yuzhi.common.annotation.EnumFormat;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * @Desc: 枚举转换序列化修饰符
 * @author sunshine
 * @date 2024-04-19
 */
@AllArgsConstructor
public class EnumTranSerializerModifier extends BeanSerializerModifier {
    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
        for (BeanPropertyWriter writer : beanProperties) {
            EnumFormat enumTranFormat = writer.getAnnotation(EnumFormat.class);
            if (enumTranFormat != null) {
                //更改序列化器
                writer.assignSerializer(new EnumTranCodeSerializer(enumTranFormat));
            }
        }
        return beanProperties;
    }
}
